package com.atom.process.cnmuseum.cj.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.atom.common.remote.user.UserUtil;
import com.atom.common.util.JsonUtil;
import com.atom.process.cnmuseum.cj.entity.task.CjDbrwTaskDO;
import com.atom.process.cnmuseum.cj.entity.task.CjGcxxTaskDO;
import com.atom.process.cnmuseum.cj.entity.task.CjMultimediaTaskDO;
import com.atom.process.cnmuseum.cj.entity.task.CjTaskDO;
import com.atom.process.cnmuseum.cj.entity.task.CjTrackmessageTaskDO;
import com.atom.process.cnmuseum.cj.entity.task.CjWhrlxxxTaskDO;
import com.atom.process.cnmuseum.cj.entity.task.CjWhsxTaskDO;
import com.atom.process.cnmuseum.cj.entity.task.CjXzxxTaskDO;
import com.atom.process.cnmuseum.cj.entity.task.CjXzyxTaskDO;
import com.atom.process.cnmuseum.cj.entity.task.CjYxxxTaskDO;
import com.atom.process.cnmuseum.cj.vo.QueryVO;
import com.atom.process.cnmuseum.cj.vo.request.DbrwRequestVO;
import com.atom.process.cnmuseum.cj.vo.request.MultimediaRequestVO;
import com.atom.process.cnmuseum.cj.vo.request.TrackmessageRequestVO;
import com.atom.process.cnmuseum.cj.vo.request.WhsxRequestVO;
import com.atom.process.cnmuseum.cj.vo.request.XzyxRequestVO;
import com.sinosoft.cnmuseum.dao.MbManageAudiofrequencyDao;
import com.sinosoft.cnmuseum.dao.MbManageImageDao;
import com.sinosoft.cnmuseum.dao.MbManageLiteratureDao;
import com.sinosoft.cnmuseum.dao.MbManageThreedimensionalmodelDao;
import com.sinosoft.cnmuseum.dao.MbManageVideoDao;
import com.sinosoft.cnmuseum.dao.MbMusicGcxxDao;
import com.sinosoft.cnmuseum.entity.MbGyByfl;
import com.sinosoft.cnmuseum.entity.MbGyCz;
import com.sinosoft.cnmuseum.entity.MbGyHsfl;
import com.sinosoft.cnmuseum.entity.MbGyMjfl;
import com.sinosoft.cnmuseum.entity.MbGyNd;
import com.sinosoft.cnmuseum.entity.MbGyYqzl;
import com.sinosoft.cnmuseum.entity.MbGyYy;
import com.sinosoft.cnmuseum.entity.MbGyZq;
import com.sinosoft.cnmuseum.entity.MbManageAudiofrequency;
import com.sinosoft.cnmuseum.entity.MbManageImage;
import com.sinosoft.cnmuseum.entity.MbManageLiterature;
import com.sinosoft.cnmuseum.entity.MbManageThreedimensionalmodel;
import com.sinosoft.cnmuseum.entity.MbManageVideo;
import com.sinosoft.cnmuseum.servlets.MbXtglBaseTableServlet;
import com.sinosoft.cnmuseum.utils.LogUtils;
import com.sinosoft.cnmuseum.utils.PageUtil;
import com.sinosoft.cnmuseum.utils.Status;
import com.sinosoft.cnmuseum.utils.StringUtils;
import com.sinosoft.commontools.record.Record;

@Service
public class CangPinService extends AbstractService {

    public Record getList(QueryVO params, HttpServletRequest request) {
        Record data = new Record();
        data.add("pageNumber", params.getPageNumber());
        data.add("offset", params.getOffset());
        data.add("limit", params.getLimit());
        data.add("pageSize", params.getPageSize());

        String processDefKey = params.getProcessDefKey();
        String taskDefKey = params.getTaskDefKey();
        Record record = new Record();
        if (taskDefKey == null || taskDefKey.equals("")) {

            record.add(Status.SATUS, Status.FAILED);
            return record;
        }
        record.add(Status.SATUS, Status.OK);
        try {
            int pagenumber = data.getIntValue(Status.PAGENUMBER);// 获取当前页
            int pagesize = data.getIntValue(Status.PAGESIZE);// 获取每页多少条数据
            HttpSession session = request.getSession(false);
            String userid = (String) session.getAttribute(Status.SESSION_USERID);
            // 采集流程数据   不是最新数据，流程有效
            String sql = "select ru.task_id,ru.process_ins_id,ru.pre_assignee,ru.is_lock,gcxx.* from atm_common_ru_task ru "
                    + "left join atm_cj_gcxx_task gcxx on ru.task_id=gcxx._TASK_ID "
                    + "where TASK_DEF_KEY = '" + taskDefKey + "'  and PROCESS_DEF_KEY='" + processDefKey + "'  ";
            String ordersql = " order by id desc ";
            String limitsql = " limit ";
            // 分配人为当前人 或者 分配人为空
            sql += " and ASSIGNEE = '" + userid + "' ";
            sql += getParams(params);
            MbMusicGcxxDao gcxxdao = new MbMusicGcxxDao();
            List<Record> countlist = gcxxdao.getBySql("select count(*) n from (" + sql + ") a");
            int total = countlist.get(0).getIntValue("n");//获得总记录数
            PageUtil page = new PageUtil(pagenumber, pagesize, total);
            limitsql += page.getStart() + "," + pagesize;
            List<Record> list = gcxxdao.getBySql(sql + ordersql + limitsql);

            for (Record r : list) {

                String[] baseTables = {MbGyHsfl.MB_GY_HSFL_TABLE_NAME, MbGyMjfl.MB_GY_MJFL_TABLE_NAME,
                        MbGyByfl.MB_GY_BYFL_TABLE_NAME, MbGyNd.MB_GY_ND_TABLE_NAME};
                String[] fields = {"class_huosa", "class_minjian", "class_bayin", "nd"};
                MbXtglBaseTableServlet baseTableServlet = new MbXtglBaseTableServlet();
                for (int i = 0; i < fields.length; i++) {
                    String f = fields[i];
                    String value = r.getStrValue(f);
                    String text = "";
                    if (StringUtils.isNoEmpty(value)) {
                        text = baseTableServlet.getNameById(baseTables[i], Integer.valueOf(value));
                    }
                    r.add(f + "_text", text);
                }
            }

            record.add(Status.ROWS, list);
            record.add(Status.TOTAL, total);
            record.add(Status.PAGENUMBER, pagenumber);
            record.add(Status.PAGESIZE, pagesize);
            record.add(Status.PAGETOTAL, page.getPagetotal());
        } catch (Exception e) {
            record.add(Status.SATUS, Status.FAILED);
            record.add(Status.MSG, "数据异常！");
        }
        return record;


//		Record record = servlet.getCjData(request, data);
//		return record;
    }

    public Record getDetail(String task_id) {
        Record result = new Record();
        CjTaskDO cj = cjTaskDao.findByTaskId(task_id);
        Record record = JsonUtil.ObjectToRecord(cj);

        CjGcxxTaskDO gcxx = cjGcxxTaskDao.findByTaskId(task_id);
        record = JsonUtil.ObjectToRecord(gcxx, record);

        CjXzxxTaskDO xzxx = cjXzxxTaskDao.findByTaskId(task_id);
        record = JsonUtil.ObjectToRecord(xzxx, record);


        CjYxxxTaskDO yxxx = cjYxxxTaskDao.findByTaskId(task_id);
        record = JsonUtil.ObjectToRecord(yxxx, record);

        CjWhrlxxxTaskDO whrlxxx = cjWhrlxxxTaskDao.findByTaskId(task_id);
        record = JsonUtil.ObjectToRecord(whrlxxx, record);


        String[] imagesByZdbh = new String[]{"formDrawing", "improveStateImage", "mediatorMaterialImage"
                , "vibrationTransferMaterialImage", "vibrationTransferStructImage", "spectrumAnalysisImage",
                "resonatorMaterialImage", "resonatorStrutImage", "spectrumImage", "spectrumAnalysisImage",
                "mediatorStructImage", "vibrationTransferStructImage", "structImage",
                "resonatorMaterialImage", "resonatorStrutImage", "spectrumImage", "spectrumAnalysisImage"
                , "mediatorStructImage", "originImage", "musicScoreImage", "historyImage", "folkloreImage", "faithImage"};
        String[] audioByZdbh = new String[]{"pitchAudio", "toneAudio", "temperamentAudio",
                "bestRegisterAudio", "toneMethodAudio", "mainPartSoundAudio", "rangeAudio", "mediatorShapeAudio",
                "mainPartSoundAudio", "faithAudio", "historyAudio", "originAudio", "folkloreAudio"};
        String[] videoByZdbh = new String[]{"voiceMechanismVideo", "provokeVideo", "structVideo",
                "techniquesVideo", "inheritTeachVideo", "techniquesVideo", "inheritTeachVideo", "faithVideo",
                "originVideo", "folkloreVideo", "historyVideo"};
        String[] ThreedimensionalmodelByZdbh = new String[]{};
        String[] LiteratureByZdbh = new String[]{};

        MbManageImageDao imagedao = new MbManageImageDao();
        for (String resource : imagesByZdbh) {
            List<Record> ls = imagedao.getBySql("select * from " + MbManageImage.MB_MANAGE_IMAGE_TABLE_NAME
                    + " where zdbh='" + record.getStrValue(resource) + "'");
            if (!ls.isEmpty()) {
                record.add(resource + "1", ls.get(0).getStrValue("title"));
            }
        }
        MbManageAudiofrequencyDao audiodao = new MbManageAudiofrequencyDao();
        for (String resource : audioByZdbh) {
            List<Record> ls = audiodao.getBySql("select * from "
                    + MbManageAudiofrequency.MB_MANAGE_AUDIOFREQUENCY_TABLE_NAME
                    + " where zdbh='" + record.getStrValue(resource) + "'");
            if (!ls.isEmpty()) {
                record.add(resource + "1", ls.get(0).getStrValue("title"));
            }
        }
        MbManageVideoDao videodao = new MbManageVideoDao();
        for (String resource : videoByZdbh) {
            List<Record> ls = videodao.getBySql("select * from " + MbManageVideo.MB_MANAGE_VIDEO_TABLE_NAME
                    + " where zdbh='"
                    + record.getStrValue(resource) + "'");
            if (!ls.isEmpty()) {
                record.add(resource + "1", ls.get(0).getStrValue("title"));
            }
        }
        MbManageThreedimensionalmodelDao tdmdao = new MbManageThreedimensionalmodelDao();
        for (String resource : ThreedimensionalmodelByZdbh) {
            List<Record> ls = tdmdao.getBySql("select * from "
                    + MbManageThreedimensionalmodel.MB_MANAGE_THREEDIMENSIONALMODEL_TABLE_NAME
                    + " where zdbh='" + record.getStrValue(resource) + "'");
            if (!ls.isEmpty()) {
                record.add(resource + "1", ls.get(0).getStrValue("title"));
            }
        }
        MbManageLiteratureDao literaturedao = new MbManageLiteratureDao();
        for (String resource : LiteratureByZdbh) {
            List<Record> ls = literaturedao.getBySql("select * from "
                    + MbManageLiterature.MB_MANAGE_LITERATURE_TABLE_NAME
                    + " where zdbh='" + record.getStrValue(resource) + "'");
            if (!ls.isEmpty()) {
                record.add(resource + "1", ls.get(0).getStrValue("title"));
            }
        }

        String[] baseTables = {MbGyHsfl.MB_GY_HSFL_TABLE_NAME, MbGyMjfl.MB_GY_MJFL_TABLE_NAME, MbGyByfl.MB_GY_BYFL_TABLE_NAME,
                MbGyYy.MB_GY_YY_TABLE_NAME, MbGyZq.MB_GY_ZQ_TABLE_NAME, MbGyCz.MB_GY_CZ_TABLE_NAME};
        String[] fields = {"class_huosa", "class_minjian", "class_bayin", "language", "useNation", "material"};
        MbXtglBaseTableServlet baseTableServlet = new MbXtglBaseTableServlet();
        for (int i = 0; i < fields.length; i++) {
            String f = fields[i];
            String value = record.getStrValue(f);
            String text = "";
            if (StringUtils.isNoEmpty(value)) {
                //字段多选情况遍历数组
                String[] singleIds = value.split(",");
                for (int j = 0; j < singleIds.length; j++) {
                    String subText = baseTableServlet.getNameById(baseTables[i], Integer.valueOf(singleIds[j]));
                    if (j!=singleIds.length-1){
                        text = text + subText+",";
                    }else {
                        text = text + subText;
                    }
                }
            }
            record.add(f + "_text", text);
        }


        if (record != null && !record.getAllFlds().isEmpty()) {
            result.add(Status.DATA, record);
            result.add(Status.SATUS, Status.OK);
        } else {
            result.add(Status.SATUS, Status.FAILED);
        }
        return result;
    }


    public Record getWhsxList(String task_id) {
        Record result = new Record();
        List<CjWhsxTaskDO> whsxlist = cjWhsxTaskDao.findByProperty("taskId", task_id);
        List<Record> rows = new ArrayList<Record>();
        String[] baseTables = {MbGyYqzl.MB_GY_YQZL_TABLE_NAME};
        String[] fields = {"instrumentType"};
        MbXtglBaseTableServlet baseTableServlet = new MbXtglBaseTableServlet();
        for (CjWhsxTaskDO whsx : whsxlist) {
            Record record = JsonUtil.ObjectToRecord(whsx);

            for (int i = 0; i < fields.length; i++) {
                String f = fields[i];
                String value = record.getStrValue(f);
                String text = "";
                if (StringUtils.isNoEmpty(value)) {
                    text = baseTableServlet.getNameById(baseTables[i], Integer.valueOf(value));
                }
                record.add(f + "_text", text);
            }

            rows.add(record);
        }
        result.add(Status.ROWS, rows);
        result.add(Status.SATUS, Status.OK);
        return result;
    }


    public Record getWhsxDetail(String id) {
        Record result = new Record();
        CjWhsxTaskDO whsx = cjWhsxTaskDao.findById(Integer.valueOf(id));
        Record record = JsonUtil.ObjectToRecord(whsx);

        String[] imagesByZdbh = new String[]{"musicScoreImage"};
        String[] videoByZdbh = new String[]{"inheritTeachVideo"};

        MbManageImageDao imagedao = new MbManageImageDao();
        for (String resource : imagesByZdbh) {
            List<Record> ls = imagedao.getBySql("select * from " + MbManageImage.MB_MANAGE_IMAGE_TABLE_NAME
                    + " where zdbh='" + record.getStrValue(resource) + "'");
            if (!ls.isEmpty()) {
                record.add(resource + "1", ls.get(0).getStrValue("title"));
            }
        }
        MbManageVideoDao videodao = new MbManageVideoDao();
        for (String resource : videoByZdbh) {
            List<Record> ls = videodao.getBySql("select * from " + MbManageVideo.MB_MANAGE_VIDEO_TABLE_NAME
                    + " where zdbh='"
                    + record.getStrValue(resource) + "'");
            if (!ls.isEmpty()) {
                record.add(resource + "1", ls.get(0).getStrValue("title"));
            }
        }

        if (record != null && !record.getAllFlds().isEmpty()) {
            result.add(Status.DATA, record);
            result.add(Status.SATUS, Status.OK);
        } else {
            result.add(Status.SATUS, Status.FAILED);
        }
        return result;
    }

    public Record saveWhsx(WhsxRequestVO whsxvo,UserUtil user) {
        CjWhsxTaskDO whsx = new CjWhsxTaskDO();
        if (whsxvo.getId() != 0) {
            whsx = cjWhsxTaskDao.findById(whsxvo.getId());
            BeanUtils.copyProperties(whsxvo, whsx, "id");
            cjWhsxTaskDao.saveOrUpdate(whsx);
            //文化事项日志更新
            LogUtils logUtils = new LogUtils();
            String desc = "文化事项"+LogUtils.MbXtglSJCJ_UPDATE_DESC;
            logUtils.addLog(String.valueOf(LogUtils.type_update), LogUtils.model_sjcj, user.getUserid(), desc); 
        }else {
            BeanUtils.copyProperties(whsxvo, whsx, "id");
            cjWhsxTaskDao.saveOrUpdate(whsx);
            //文化事项日志新增
            LogUtils logUtils = new LogUtils();
            String desc = "文化事项"+LogUtils.MbXtglSJCJ_INSERT_DESC;
            logUtils.addLog(String.valueOf(LogUtils.type_insert), LogUtils.model_sjcj, user.getUserid(), desc);
        }

        Record result = new Record();
        result.add(Status.SATUS, Status.OK);
        return result;

    }

    public Record deleteWhsx(String id,UserUtil user) {
        CjWhsxTaskDO whsx = cjWhsxTaskDao.findById(Integer.valueOf(id));
        cjWhsxTaskDao.delete(whsx);
        //文化事项删除日志
        LogUtils logUtils = new LogUtils();
        String desc = "文化事项"+LogUtils.MbXtglSJCJ_DELETE_DESC;
        logUtils.addLog(String.valueOf(LogUtils.type_delete), LogUtils.model_sjcj, user.getUserid(), desc);
        Record result = new Record();
        result.add(Status.SATUS, Status.OK);
        return result;
    }

    public Record getDbrwList(String task_id) {
        Record result = new Record();
        List<CjDbrwTaskDO> dbrwlist = cjDbrwTaskDao.findByProperty("taskId", task_id);
        List<Record> rows = new ArrayList<Record>();
        for (CjDbrwTaskDO dbrw : dbrwlist) {
            Record record = JsonUtil.ObjectToRecord(dbrw);
            rows.add(record);
        }
        result.add(Status.ROWS, rows);
        result.add(Status.SATUS, Status.OK);
        return result;
    }


    public Record getDbrwDetail(String id) {
        Record result = new Record();
        CjDbrwTaskDO dbrw = cjDbrwTaskDao.findById(Integer.valueOf(id));
        Record record = JsonUtil.ObjectToRecord(dbrw);

        if (record != null && !record.getAllFlds().isEmpty()) {
            result.add(Status.DATA, record);
            result.add(Status.SATUS, Status.OK);
        } else {
            result.add(Status.SATUS, Status.FAILED);
        }
        return result;
    }

    public Record saveDbrw(DbrwRequestVO dbrwvo,UserUtil user) {
        CjDbrwTaskDO dbrw = new CjDbrwTaskDO();
        if (dbrwvo.getId() != 0) {
            dbrw = cjDbrwTaskDao.findById(dbrwvo.getId());
            BeanUtils.copyProperties(dbrwvo, dbrw, "id");
            cjDbrwTaskDao.saveOrUpdate(dbrw);
            //代表人物更新日志
            LogUtils logUtils = new LogUtils();
            String desc = "代表人物"+LogUtils.MbXtglSJCJ_UPDATE_DESC;
            logUtils.addLog(String.valueOf(LogUtils.type_update), LogUtils.model_sjcj, user.getUserid(), desc);
        }else {
            BeanUtils.copyProperties(dbrwvo, dbrw, "id");
            cjDbrwTaskDao.saveOrUpdate(dbrw);
            //代表人物新增日志
            LogUtils logUtils = new LogUtils();
            String desc = "代表人物"+LogUtils.MbXtglSJCJ_INSERT_DESC;
            logUtils.addLog(String.valueOf(LogUtils.type_insert), LogUtils.model_sjcj, user.getUserid(), desc);
            
        }

        Record result = new Record();
        result.add(Status.SATUS, Status.OK);
        return result;

    }

    public Record deleteDbrw(String id,UserUtil user) {
        CjDbrwTaskDO dbrw = cjDbrwTaskDao.findById(Integer.valueOf(id));
        cjDbrwTaskDao.delete(dbrw);
        //代表人物删除日志
        LogUtils logUtils = new LogUtils();
        String desc = "代表人物"+LogUtils.MbXtglSJCJ_DELETE_DESC;
        logUtils.addLog(String.valueOf(LogUtils.type_delete), LogUtils.model_sjcj, user.getUserid(), desc);
        Record result = new Record();
        result.add(Status.SATUS, Status.OK);
        return result;
    }

    private String getParams(Record data) {

        String where = "";
        // 查询条件,藏品号
        String collection_code = data.getStrValue("collection_code");
        if (StringUtils.isNoEmpty(collection_code)) {
            where += " and collection_code like '%" + collection_code + "%' ";
        }

        // 查询条件,根据名称模糊查询
        String title = data.getStrValue("yqmc");
        if (StringUtils.isNoEmpty(title)) {
            where += " and title like '%" + title + "%' ";
        }

        // 查询条件,根据八音分类
        String class_bayin = data.getStrValue("class_bayin");
        if (StringUtils.isNoEmpty(class_bayin)) {
            where += " and class_bayin =" + class_bayin;
            ;
        }

        // 查询条件,根据民间分类
        String class_minjian = data.getStrValue("class_minjian");
        String class_minjian_showchild = data.getStrValue("class_minjian_showchild");
        if (StringUtils.isNoEmpty(class_minjian_showchild)) {
            MbXtglBaseTableServlet servlet = new MbXtglBaseTableServlet();
            if (StringUtils.isNoEmpty(class_minjian)) {
                class_minjian = servlet.getAllChildrenId4Select(Integer.valueOf(class_minjian), "mb_gy_mjfl");
                where += " and class_minjian in (" + class_minjian + ")";
            }
        } else {
            if (StringUtils.isNoEmpty(class_minjian)) {
                where += " and class_minjian =" + class_minjian;
                ;
            }
        }

        String class_huosa = data.getStrValue("class_huosa");
        String class_huosa_showchild = data.getStrValue("class_huosa_showchild");
        if (StringUtils.isNoEmpty(class_huosa_showchild)) {
            MbXtglBaseTableServlet servlet = new MbXtglBaseTableServlet();
            if (StringUtils.isNoEmpty(class_huosa)) {
                class_huosa = servlet.getAllChildrenId4Select(Integer.valueOf(class_huosa), "mb_gy_hsfl");
                where += " and class_huosa in (" + class_huosa + ")";
            }
        } else {
            if (StringUtils.isNoEmpty(class_huosa)) {
                where += " and class_huosa =" + class_huosa;
                ;
            }
        }
        // 查询条件,根据自称模糊查询
        String title_self = data.getStrValue("title_self");
        if (StringUtils.isNoEmpty(title_self)) {
            where += " and title_self like '%" + title_self + "%' ";
        }

        // 查询条件,根据关键字模糊查询
        String gjz = data.getStrValue("gjz");
        if (StringUtils.isNoEmpty(gjz)) {
            where += " and gjz like '%" + gjz + "%' ";
        }

        return where;
    }
    private String getParams(QueryVO data) {

        String where = "";
        // 查询条件,藏品号
        String collection_code = data.getCollection_code();
        if (StringUtils.isNoEmpty(collection_code)) {
            where += " and collection_code like '%" + collection_code + "%' ";
        }

        // 查询条件,根据名称模糊查询
        String title = data.getYqmc();
        if (StringUtils.isNoEmpty(title)) {
            where += " and title like '%" + title + "%' ";
        }

        // 查询条件,根据八音分类
        String class_bayin = data.getClass_bayin();
        if (StringUtils.isNoEmpty(class_bayin)) {
            where += " and class_bayin =" + class_bayin;
            ;
        }

        // 查询条件,根据民间分类
        String class_minjian = data.getClass_minjian();
        String class_minjian_showchild = data.getClass_minjian_showchild();
        if (StringUtils.isNoEmpty(class_minjian_showchild)) {
            MbXtglBaseTableServlet servlet = new MbXtglBaseTableServlet();
            if (StringUtils.isNoEmpty(class_minjian)) {
                class_minjian = servlet.getAllChildrenId4Select(Integer.valueOf(class_minjian), "mb_gy_mjfl");
                where += " and class_minjian in (" + class_minjian + ")";
            }
        } else {
            if (StringUtils.isNoEmpty(class_minjian)) {
                where += " and class_minjian =" + class_minjian;
                ;
            }
        }

        String class_huosa = data.getClass_huosa();
        String class_huosa_showchild = data.getClass_huosa_showchild();
        if (StringUtils.isNoEmpty(class_huosa_showchild)) {
            MbXtglBaseTableServlet servlet = new MbXtglBaseTableServlet();
            if (StringUtils.isNoEmpty(class_huosa)) {
                class_huosa = servlet.getAllChildrenId4Select(Integer.valueOf(class_huosa), "mb_gy_hsfl");
                where += " and class_huosa in (" + class_huosa + ")";
            }
        } else {
            if (StringUtils.isNoEmpty(class_huosa)) {
                where += " and class_huosa =" + class_huosa;
                ;
            }
        }
//        // 查询条件,根据自称模糊查询
//        String title_self = data.gettitle_self;
//        if (StringUtils.isNoEmpty(title_self)) {
//            where += " and title_self like '%" + title_self + "%' ";
//        }
//
//        // 查询条件,根据关键字模糊查询
//        String gjz = data.getStrValue("gjz");
//        if (StringUtils.isNoEmpty(gjz)) {
//            where += " and gjz like '%" + gjz + "%' ";
//        }

        return where;
    }

	public Record getMultimediaList(String task_id) {
		// TODO Auto-generated method stub
        Record result = new Record();
        List<CjMultimediaTaskDO> multimedialist = cjMultimediaTaskDao.findByProperty("taskId", task_id);
        List<Record> rows = new ArrayList<Record>();
        for (CjMultimediaTaskDO multimedia : multimedialist) {
            Record record = JsonUtil.ObjectToRecord(multimedia);
            rows.add(record);
        }
        result.add(Status.ROWS, rows);
        result.add(Status.SATUS, Status.OK);
        return result;
	}

	public Record getMultimediaDetail(String id) {
		// TODO Auto-generated method stub
        Record result = new Record();
        CjMultimediaTaskDO multimedia = cjMultimediaTaskDao.findById(Integer.valueOf(id));
        Record record = JsonUtil.ObjectToRecord(multimedia);

        if (record != null && !record.getAllFlds().isEmpty()) {
            result.add(Status.DATA, record);
            result.add(Status.SATUS, Status.OK);
        } else {
            result.add(Status.SATUS, Status.FAILED);
        }
        return result;
	}

	public Record saveMultimedia(MultimediaRequestVO multimediavo, UserUtil user) {
		// TODO Auto-generated method stub
        CjMultimediaTaskDO multimedia = new CjMultimediaTaskDO();
        if (multimediavo.getId() != 0) {
        	multimedia = cjMultimediaTaskDao.findById(multimediavo.getId());
            BeanUtils.copyProperties(multimediavo, multimedia, "id");
            cjMultimediaTaskDao.saveOrUpdate(multimedia);
            //多媒体信息更新日志
            LogUtils logUtils = new LogUtils();
            String desc = "多媒体信息"+LogUtils.MbXtglSJCJ_UPDATE_DESC;
            logUtils.addLog(String.valueOf(LogUtils.type_update), LogUtils.model_sjcj, user.getUserid(), desc);
        }else {
            BeanUtils.copyProperties(multimediavo, multimedia, "id");
            cjMultimediaTaskDao.saveOrUpdate(multimedia);
            //多媒体信息新增日志
            LogUtils logUtils = new LogUtils();
            String desc = "多媒体信息"+LogUtils.MbXtglSJCJ_INSERT_DESC;
            logUtils.addLog(String.valueOf(LogUtils.type_insert), LogUtils.model_sjcj, user.getUserid(), desc);
            
        }

        Record result = new Record();
        result.add(Status.SATUS, Status.OK);
        return result;

	}

	public Record deleteMultimedia(String id, UserUtil user) {
		// TODO Auto-generated method stub
		CjMultimediaTaskDO multimedia = cjMultimediaTaskDao.findById(Integer.valueOf(id));
		cjMultimediaTaskDao.delete(multimedia);
        //多媒体信息删除日志
        LogUtils logUtils = new LogUtils();
        String desc = "多媒体信息"+LogUtils.MbXtglSJCJ_DELETE_DESC;
        logUtils.addLog(String.valueOf(LogUtils.type_delete), LogUtils.model_sjcj, user.getUserid(), desc);
        Record result = new Record();
        result.add(Status.SATUS, Status.OK);
        return result;
	}

	public Record getTrackmessageList(String task_id) {
		// TODO Auto-generated method stub
        Record result = new Record();
        List<CjTrackmessageTaskDO> trackmessagelist = cjTrackmessageTaskDao.findByProperty("taskId", task_id);
        List<Record> rows = new ArrayList<Record>();
        for (CjTrackmessageTaskDO trackmessage : trackmessagelist) {
            Record record = JsonUtil.ObjectToRecord(trackmessage);
            rows.add(record);
        }
        result.add(Status.ROWS, rows);
        result.add(Status.SATUS, Status.OK);
        return result;
	}

	public Record getTrackmessageDetail(String id) {
		// TODO Auto-generated method stub
        Record result = new Record();
        CjTrackmessageTaskDO trackmessage = cjTrackmessageTaskDao.findById(Integer.valueOf(id));
        Record record = JsonUtil.ObjectToRecord(trackmessage);

        if (record != null && !record.getAllFlds().isEmpty()) {
            result.add(Status.DATA, record);
            result.add(Status.SATUS, Status.OK);
        } else {
            result.add(Status.SATUS, Status.FAILED);
        }
        return result;
	}

	public Record saveTrackmessage(TrackmessageRequestVO trackmessagevo, UserUtil user) {
		// TODO Auto-generated method stub
        CjTrackmessageTaskDO trackmessage = new CjTrackmessageTaskDO();
        if (trackmessagevo.getId() != 0) {
        	trackmessage = cjTrackmessageTaskDao.findById(trackmessagevo.getId());
            BeanUtils.copyProperties(trackmessagevo, trackmessage, "id");
            cjTrackmessageTaskDao.saveOrUpdate(trackmessage);
            //曲目信息更新日志
            LogUtils logUtils = new LogUtils();
            String desc = "曲目信息"+LogUtils.MbXtglSJCJ_UPDATE_DESC;
            logUtils.addLog(String.valueOf(LogUtils.type_update), LogUtils.model_sjcj, user.getUserid(), desc);
        }else {
            BeanUtils.copyProperties(trackmessagevo, trackmessage, "id");
            cjTrackmessageTaskDao.saveOrUpdate(trackmessage);
            //曲目信息新增日志
            LogUtils logUtils = new LogUtils();
            String desc = "曲目信息"+LogUtils.MbXtglSJCJ_INSERT_DESC;
            logUtils.addLog(String.valueOf(LogUtils.type_insert), LogUtils.model_sjcj, user.getUserid(), desc);
            
        }

        Record result = new Record();
        result.add(Status.SATUS, Status.OK);
        return result;
	}

	public Record deleteTrackmessage(String id, UserUtil user) {
		// TODO Auto-generated method stub
		CjTrackmessageTaskDO trackmessage = cjTrackmessageTaskDao.findById(Integer.valueOf(id));
		cjTrackmessageTaskDao.delete(trackmessage);
        //曲目信息删除日志
        LogUtils logUtils = new LogUtils();
        String desc = "曲目信息"+LogUtils.MbXtglSJCJ_DELETE_DESC;
        logUtils.addLog(String.valueOf(LogUtils.type_delete), LogUtils.model_sjcj, user.getUserid(), desc);
        Record result = new Record();
        result.add(Status.SATUS, Status.OK);
        return result;
	}

	public Record getXzyxList(String task_id, String filesign) {
		// TODO Auto-generated method stub
        Record result = new Record();        
        Map<String, Object> vars= new HashMap<String, Object>();
        vars.put("taskId", task_id);
        vars.put("filesign", filesign);
        List<CjXzyxTaskDO> xzyxlist = cjXzyxTaskDao.findByProperties(vars);
        List<Record> rows = new ArrayList<Record>();
        for (CjXzyxTaskDO xzyx : xzyxlist) {
            Record record = JsonUtil.ObjectToRecord(xzyx);
            rows.add(record);
        }
        result.add(Status.ROWS, rows);
        result.add(Status.SATUS, Status.OK);
        return result;
	}

	public Record getXzyxDetail(String id) {
		// TODO Auto-generated method stub
        Record result = new Record();
        CjXzyxTaskDO xzyx = cjXzyxTaskDao.findById(Integer.valueOf(id));
        Record record = JsonUtil.ObjectToRecord(xzyx);
        if (record != null && !record.getAllFlds().isEmpty()) {
            result.add(Status.DATA, record);
            result.add(Status.SATUS, Status.OK);
        } else {
            result.add(Status.SATUS, Status.FAILED);
        }
        return result;
	}

	public Record saveXzyx(XzyxRequestVO xzyxvo, UserUtil user, String filesign) {
		// TODO Auto-generated method stub
		CjXzyxTaskDO xzyx = new CjXzyxTaskDO();
        if (xzyxvo.getId() != 0) {
        	xzyx = cjXzyxTaskDao.findById(xzyxvo.getId());
            BeanUtils.copyProperties(xzyxvo, xzyx, "id");
            xzyx.setFilesign(filesign);
            cjXzyxTaskDao.saveOrUpdate(xzyx);
            //形制文件   音响文件  更新日志
            LogUtils logUtils = new LogUtils();
            String mc="";
            if("xz".equals(filesign)) {
            	mc="形制文件信息";
            } 
            if("yx".equals(filesign)){
            	mc="音响文件信息";
            }
            String desc = mc+LogUtils.MbXtglSJCJ_UPDATE_DESC;
            logUtils.addLog(String.valueOf(LogUtils.type_update), LogUtils.model_sjcj, user.getUserid(), desc);
        }else {
            BeanUtils.copyProperties(xzyxvo, xzyx, "id");
            xzyx.setFilesign(filesign);
            cjXzyxTaskDao.saveOrUpdate(xzyx);
            //形制文件  音响文件  新增日志
            LogUtils logUtils = new LogUtils();
            String mc="";
            if("xz".equals(filesign)) {
            	mc="形制文件信息";
            } 
            if("yx".equals(filesign)){
            	mc="音响文件信息";
            }
            String desc = mc+LogUtils.MbXtglSJCJ_INSERT_DESC;
            logUtils.addLog(String.valueOf(LogUtils.type_insert), LogUtils.model_sjcj, user.getUserid(), desc);
            
        }

        Record result = new Record();
        result.add(Status.SATUS, Status.OK);
        return result;
	}

	public Record deleteXzyx(String id, UserUtil user, String filesign) {
		// TODO Auto-generated method stub
		CjXzyxTaskDO xzyx = cjXzyxTaskDao.findById(Integer.valueOf(id));
		cjXzyxTaskDao.delete(xzyx);
        //曲目信息删除日志
        LogUtils logUtils = new LogUtils();
        String mc="";
        if("xz".equals(filesign)) {
        	mc="形制文件信息";
        } 
        if("yx".equals(filesign)){
        	mc="音响文件信息";
        }
        String desc = mc+LogUtils.MbXtglSJCJ_DELETE_DESC;
        logUtils.addLog(String.valueOf(LogUtils.type_delete), LogUtils.model_sjcj, user.getUserid(), desc);
        Record result = new Record();
        result.add(Status.SATUS, Status.OK);
        return result;
	}


}
